/**
 * Copyright (c) 2019 xstgongyi.org
 * All rights reserved.
 */
package org.xstgongyi.eduaid.manager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.xstgongyi.eduaid.dao.BankAccountMapper;
import org.xstgongyi.eduaid.dataobject.BankAccountDO;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author oldcrane
 * @created 2019-04-22
 */
@Service
@CacheConfig(cacheNames = "bank-account")
public class BankAccountManager {

    @Autowired
    private BankAccountMapper accountDao;

    @Cacheable(key = "#id")
    public BankAccountDO getById(Integer id) {
        return accountDao.getById(id);
    }

    @Cacheable(key = "'list-all'")
    public List<BankAccountDO> listAll() {
        return accountDao.list(null);
    }

    @CacheEvict(allEntries = true)
    public void insert(BankAccountDO account) {
        LocalDateTime now = LocalDateTime.now();
        account.setGmtCreated(now);
        account.setGmtUpdated(now);
        accountDao.insert(account);
    }

    @CacheEvict(allEntries = true)
    public void update(BankAccountDO account) {
        account.setGmtUpdated(LocalDateTime.now());
        accountDao.update(account);
    }

}
